package com.tepth.maintenancesystem.adapter;

import android.content.Context;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.tepth.maintenancesystem.R;
import com.tepth.maintenancesystem.bean.VehicleMaintain;
import com.tepth.maintenancesystem.untils.Untils;

import java.util.List;

/**
 * Created by LHQ on 2016/12/27.
 */

public class MaintainHistoryAdapter extends RecyclerView.Adapter<MaintainHistoryAdapter.ViewHolder> {

    private List<VehicleMaintain> datas;
    private Context context;
    private HistoryItemClickListener myItemClickListener;

    public MaintainHistoryAdapter(Context context, List<VehicleMaintain> datas) {
        this.datas = datas;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater
                .from(parent.getContext())
                .inflate(R.layout.vehicle_maintain_history_item, parent, false);
        return new ViewHolder(v, context);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        VehicleMaintain obj = datas.get(position);
        holder.setHistory_maintain_name(obj.getCompletedPersonName());
        holder.setHistory_maintain_time(obj.getCompletedTime());
        holder.setHistory_plan_time(obj.getPlanEndTime());
        if (obj.getWorkStatusCode().equals("FinishWork") || obj.getWorkStatusCode().equals("CompletedWork")) {//完成
            holder.setHistory_vehicle_img(R.drawable.vehicle_green);
            holder.setHistory_vehicle_img_text(R.drawable.done_text);
        } else if (obj.getWorkStatusCode().equals("UnderWork")) {//作业中
            holder.setHistory_vehicle_img(R.drawable.vehicle_red);
            holder.setHistory_vehicle_img_text(R.drawable.doing_text);
        } else {//FinishPlan 未开始
            holder.setHistory_vehicle_img(R.drawable.vehicle_yellow);
            holder.setHistory_vehicle_img_text(R.drawable.ready_text);
        }
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public void setOnItemClickListener(HistoryItemClickListener listener) {
        myItemClickListener = listener;
    }//绑定MainActivity传进来的点击监听器

    public class ViewHolder extends RecyclerView.ViewHolder {

        private AppCompatImageView history_vehicle_img;
        private AppCompatTextView history_maintain_time;
        private AppCompatTextView history_maintain_name;
        private AppCompatTextView history_plan_time;
        private AppCompatImageView history_vehicle_img_text;
        private Context context;

        public ViewHolder(View itemView, Context context) {
            super(itemView);
            history_vehicle_img = (AppCompatImageView) itemView.findViewById(R.id.history_vehicle_img);
            history_maintain_time = (AppCompatTextView) itemView.findViewById(R.id.history_maintain_time);
            history_maintain_name = (AppCompatTextView) itemView.findViewById(R.id.history_maintain_name);
            history_plan_time = (AppCompatTextView) itemView.findViewById(R.id.history_plan_time);
            history_vehicle_img_text = (AppCompatImageView) itemView.findViewById(R.id.history_vehicle_img_text);
            this.context = context;
            itemView.setOnClickListener(new View.OnClickListener() {//ItemOnclick事件
                @Override
                public void onClick(View view) {
                    if (myItemClickListener != null)
                        myItemClickListener.onItemClick(view, getAdapterPosition());
                }
            });
        }

        public void setHistory_vehicle_img(int imageId) {
            this.history_vehicle_img.setImageDrawable(Untils.getDrawable(context, imageId));
        }

        public void setHistory_maintain_time(String time) {
            if (time.equals(""))
                this.history_maintain_time.setText("保养时间: 无");
            else
                this.history_maintain_time.setText("保养: " + time);

        }

        public void setHistory_maintain_name(String name) {
            if (name.equals("") || name.equals("null")) {
                this.history_maintain_name.setVisibility(View.GONE);
            } else {
                this.history_maintain_name.setText(name);
                this.history_maintain_name.setVisibility(View.VISIBLE);
            }
        }

        public void setHistory_plan_time(String time) {
            this.history_plan_time.setText("计划: " + time);
        }

        public void setHistory_vehicle_img_text(int imageId) {
            this.history_vehicle_img_text.setImageDrawable(Untils.getDrawable(context, imageId));
        }
    }
}
